from aiohttp import web
import asyncio
import json

class Handler:
    def __init__(self,game_type):
        self.game_type=game_type
        if self.game_type=='bac':
           
            self.Cards={
                'p1':'',
                'p2':'',
                'p3':'',
                'p4':'',
                'p5':'',
                'p6':''
            }
        elif  self.game_type=='lh':
            self.Cards={
                'p1':'',
                'p2':''
            }

    async def handle(self, request):
        return web.json_response({'statue': 1,'msg':"ok",'data': self.Cards})
    async def clearHandle(self, request):
        if self.game_type=='bac':
            self.Cards={
                'p1':'',
                'p2':'',
                'p3':'',
                'p4':'',
                'p5':'',
                'p6':''
            }
        elif  self.game_type=='lh':
            self.Cards={
                'p1':'',
                'p2':''
            }
        return web.json_response({'statue': 1,'msg':"ok"})

class HttpServer:
    def __init__(self,PortNum,game_type):
        self.PortNum=PortNum
        self.handler = Handler(game_type)
        self.game_type=game_type
        self.handler.game_type=game_type
        
    def PostCardData(self,card_name,card_value):
        #1 黑桃
        #2 红桃
        #3 梅花
        #4 方块
        suit_card_number=""
        rank_card_number=""
        suit=card_value['suit'].lower()
        rank=card_value['rank'].lower()
        if suit=="s":
            suit_card_number="1"
        elif suit=="d":
            suit_card_number="4"
        elif suit=="c":
            suit_card_number="3"
        elif suit=="h":
            suit_card_number="2"
        #'Ace','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King'
        if rank=="a":
            rank_card_number="010"
        elif rank=="2":
            rank_card_number="020"
        elif rank=="3":
            rank_card_number="030"
        elif rank=="4":
            rank_card_number="040"
        elif rank=="5":
            rank_card_number="050"
        elif rank=="6":
            rank_card_number="060"
        elif rank=="7":
            rank_card_number="070"
        elif rank=="8":
            rank_card_number="080"
        elif rank=="9":
            rank_card_number="090"
        elif rank=="10":
            rank_card_number="100"
        elif rank=="j":
            rank_card_number="110"
        elif rank=="q":
            rank_card_number="120"
        elif rank=="k":
            rank_card_number="130"
        if suit_card_number!="" and rank_card_number!="":
            self.handler.Cards[card_name]=suit_card_number+rank_card_number

    def Start(self):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)
        self.app = web.Application()
        self.app.add_routes([web.get('/getcard', self.handler.handle),
                        web.get('/clear', self.handler.clearHandle)])
        web.run_app(self.app, host='0.0.0.0', port=self.PortNum)
    def fail(self):
        1/0
    def Stop(self):
        #self.loop.run_until_complete(self.app.shutdown())
        #self.loop.run_until_complete(self.handler.shutdown(60.0))
        #self.loop.run_until_complete(self.app.cleanup())
        #self.loop.close()
        fut = self.loop.create_task(self.fail)
        self.loop.run_until_complete(fut)  # raises ZeroDivisionError

